package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	AddTrace()
}

func AddTrace() {
	defer Trace("AddTrace", Start())
	fmt.Println("start execute function")
	// 休眠10~100ms 模拟运行耗时
	rand.Seed(time.Now().UnixNano())
	duration := (10 + time.Duration(rand.Int63n(90))) * time.Millisecond
	fmt.Printf("duration=%d\n", duration)
	time.Sleep(duration)
	fmt.Println("end execute function")
}

const PATTERN = "2006-01-02 15:04:05.000"

func Start() int64 {
	return time.Now().UnixNano()
}

func Trace(name string, start int64) {
	now := time.Now()
	cost := (now.UnixNano() - start) / 1000000
	fmt.Printf("%s function=%s cost time is [%d]ms\n", now.Format(PATTERN), name, cost)
}